/**
 * Copyright (C) 2011 Audit Logger Project. All rights reserved.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.auditlogger.security;

import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

class BlowfishEncryptorDecryptor extends EncryptorDecryptor {
	
	private static final Log logger = LogFactory.getLog(EncryptorDecryptor.class);
	
	@Override
	protected SecretKeySpec prepareSecretKeySpec() {
		SecretKeySpec result = new SecretKeySpec(this.getConfigurator().getMasterKey(), 
				this.getConfigurator().getEncryptionAlgorithm());
		return result;
	}

	@Override
	protected Cipher prepareCipher() throws EncryptionException {
		try {
			Cipher cipher = Cipher.getInstance(this.getConfigurator().getEncryptionCipherTransformation());
			logger.info("Initialized cipher with transformation: " 
					+ this.getConfigurator().getEncryptionCipherTransformation());
			return cipher;
		} catch (NoSuchAlgorithmException e) {
			logger.error(e.getMessage(), e);
			throw new EncryptionException (e);
		} catch (NoSuchPaddingException e) {
			logger.error(e.getMessage(), e);
			throw new EncryptionException(e);
		}
	}
}
